package com.my;

import org.apache.commons.fileupload.FileItem;
import org.apache.commons.fileupload.FileUploadException;
import org.apache.commons.fileupload.disk.DiskFileItemFactory;
import org.apache.commons.fileupload.servlet.ServletFileUpload;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.*;
import java.util.List;

/**
 * Created by YJH on 2017/7/14 11:18.
 */
public class UploadServlet extends HttpServlet {

    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        //1.的到FileItem的集合items
        //为基于磁盘的文件项创建工厂
        DiskFileItemFactory factory = new DiskFileItemFactory();

        //设置工厂约束
        factory.setSizeThreshold(1024 * 500);
        File tempDir = new File("d:\\temp");
        factory.setRepository(tempDir);

        //创建一个新的文件上传处理器
        ServletFileUpload upload = new ServletFileUpload(factory);

        //设置整体请求大小约束
        upload.setSizeMax(1024 * 1024 * 5); //5M

        //解析请求
        List<FileItem> items = null;
        try {
            items = upload.parseRequest(request);
        } catch (FileUploadException e) {
            e.printStackTrace();
        }

        //2.遍历items
        for (FileItem item : items) {
            //若是一个一般般的表单域，打印信息
            if (item.isFormField()) {
                String name = item.getFieldName();
                String value = item.getString();
                System.out.println("name = " + name + "，value = " + value);
            } else {    //若是文件域咋把文件保存到d:\\temp目录下
                String fieldName = item.getFieldName();
                String fileName = item.getName();
                String contentType = item.getContentType();
                boolean isInMemory = item.isInMemory();
                long sizeInBytes = item.getSize();

                System.out.println("fieldName = " + fieldName
                        + "\nfileName = " + fileName
                        + "\ncontentType = " + contentType
                        + "\nsizeInBytes = " + sizeInBytes);

                InputStream in = item.getInputStream();
                byte[] buffer = new byte[1024];
                int len = 0;

                fileName = "d:\\temp\\" + fileName;
                System.out.println("fileName = " + fileName);
                OutputStream out = new FileOutputStream(fileName);

                while ((len = in.read(buffer)) != -1) {
                    out.write(buffer, 0, len);
                }
                out.close();
                in.close();
            }
        }


//        //1.获取请求信息
//        InputStream in = request.getInputStream();
//        Reader reader = new InputStreamReader(in);
//        BufferedReader bufferedReader = new BufferedReader(reader);
//        String str;
//        while ((str = bufferedReader.readLine()) != null) {
//            System.out.println(str);
//        }
    }
}
